package com.dmall.manager.service.impl;

import com.dmall.jedis.JedisClient;
import com.dmall.manager.mapper.ItemDescMapper;
import com.dmall.manager.mapper.ItemMapper;
import com.dmall.manager.model.ItemModel;
import com.dmall.pojo.TbItem;
import com.dmall.pojo.TbItemDesc;
import com.dmall.manager.service.ItemService;
import com.dmall.framework.result.PageResult;
import com.dmall.framework.result.DmallResult;
import com.dmall.utils.IDUtils;
import com.dmall.utils.JsonUtils;
import com.dmall.utils.RedisUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @author Chen 2017/9/22
 */
@Service
public class ItemServiceImpl implements ItemService{
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;
    @Autowired
    private JedisClient jedisClient;

    /**
     * 根据商品id取出商品信息
     * @param itemId
     * @return
     */
    @Override
    public TbItem getItemById(long itemId) {
        try {
            String json = jedisClient.get(RedisUtils.itemBASEKey(itemId));
            if(StringUtils.isNotBlank(json)){
                TbItem tbItem = JsonUtils.jsonToPojo(json, TbItem.class);
                return tbItem;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        ItemMapper itemMapper = sqlSessionTemplate.getMapper(ItemMapper.class);
        //缓存中没有，查询数据库
        TbItem tbItem = itemMapper.selectByPrimaryKey(itemId);
        try {
            jedisClient.set(RedisUtils.itemBASEKey(itemId),JsonUtils.objectToJson(tbItem));
            //设置过期时间
            jedisClient.expire(RedisUtils.itemBASEKey(itemId),3600);

        }catch (Exception e){
            e.printStackTrace();
        }
        return tbItem;
    }

    /**
     * 查询商品列表
     * @param page
     * @param rows
     * @return
     */
    @Override
    public PageResult getItemList(int page, int rows) {
        ItemMapper itemMapper = sqlSessionTemplate.getMapper(ItemMapper.class);
        Page<Object> p = PageHelper.startPage(page, rows);
        List<TbItem> list = itemMapper.selectAll();
        return new PageResult(p.getTotal(),list);
    }

    /**
     * 新增一个商品
     * @param item
     * @param desc
     * @return
     */
    @Override
    public DmallResult addItem(TbItem item, String desc) {
        ItemMapper itemMapper = sqlSessionTemplate.getMapper(ItemMapper.class);
        ItemDescMapper itemDescMapper = sqlSessionTemplate.getMapper(ItemDescMapper.class);
        final long itemId = IDUtils.genItemId();
        item.setId(itemId);
        item.setTitle(item.getTitle());
        item.setPrice(item.getPrice());
        item.setNum(item.getNum());
        item.setBarcode(item.getBarcode());
        item.setImage(item.getImage());
        item.setCid(item.getCid());
        item.setStatus((byte) 1);
        item.setCreated(new Date());
        item.setUpdated(new Date());
        item.setImage(item.getImage());

        itemMapper.insert(item);
        TbItemDesc itemDesc = new TbItemDesc();
        itemDesc.setItemId(itemId);
        itemDesc.setItemDesc(desc);
        itemDesc.setCreated(new Date());
        itemDesc.setUpdated(new Date());
        itemDescMapper.insert(itemDesc);

        return DmallResult.ok(itemId);
    }

    /**
     * 根据Id查询商品详情
     * @param itemId
     * @return
     */
    @Override
    public TbItemDesc getItemDescById(long itemId) {
        ItemDescMapper itemDescMapper = sqlSessionTemplate.getMapper(ItemDescMapper.class);
        TbItemDesc tbItemDesc = new TbItemDesc();
        try {
            String json = jedisClient.get(RedisUtils.itemDESCKey(itemId));
            if(StringUtils.isNotBlank(json)){
                tbItemDesc = JsonUtils.jsonToPojo(json, TbItemDesc.class);
                return tbItemDesc;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        Example example = new Example(TbItemDesc.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId",itemId);
        List<TbItemDesc> tbItemDescs = itemDescMapper.selectByExample(example);
        if(tbItemDescs !=null &&tbItemDescs.size()>0){
            tbItemDesc = tbItemDescs.get(0);
        }
        try {
            jedisClient.set(RedisUtils.itemDESCKey(itemId),JsonUtils.objectToJson(tbItemDesc));
            //设置过期时间
            jedisClient.expire(RedisUtils.itemDESCKey(itemId),3600);
        }catch (Exception e){
            e.printStackTrace();
        }
        return tbItemDesc;
    }

    /**
     * 获取所有商品列表
     * @return
     */
    @Override
    public List<ItemModel> getItemList() {
        ItemMapper itemMapper = sqlSessionTemplate.getMapper(ItemMapper.class);
        return itemMapper.getItemList();
    }

    @Override
    public ItemModel getItemModelById(long itemId) {
        ItemMapper itemMapper = sqlSessionTemplate.getMapper(ItemMapper.class);
        return itemMapper.getItemById(itemId);
    }
}
